
#include "stdafx.h"
#include "stdlib.h"
#include "CodeAnalysis.h"


// part of these codes from lopcodes.h
/* ORDER OP */

const char *const luaP_opnames[] = {
  "MOVE",
  "LOADK",
  "LOADBOOL",
  "LOADNIL",
  "GETUPVAL",
  "GETGLOBAL",
  "GETTABLE",
  "SETGLOBAL",
  "SETUPVAL",
  "SETTABLE",
  "NEWTABLE",
  "SELF",
  "ADD",
  "SUB",
  "MUL",
  "DIV",
  "MOD",
  "POW",
  "UNM",
  "NOT",
  "LEN",
  "CONCAT",
  "JMP",
  "EQ",
  "LT",
  "LE",
  "TEST",
  "TESTSET",
  "CALL",
  "TAILCALL",
  "RETURN",
  "FORLOOP",
  "FORPREP",
  "TFORLOOP",
  "SETLIST",
  "CLOSE",
  "CLOSURE",
  "VARARG",
  NULL
};

OpDef OpCodes_Define[] = 
{
	{0,		OP_MOVE,		OpArgU,	OpArgR,	OpArgN,	iABC},		/* OP_MOVE */
	{1,		OP_LOADK,		OpArgU,	OpArgK,	OpArgN,	iABx},		/* OP_LOADK */
	{2,		OP_LOADBOOL,	OpArgU, OpArgU, OpArgU,	iABC},		/* OP_LOADBOOL */
	{3,		OP_LOADNIL,		OpArgU,	OpArgR,	OpArgN,	iABC},		/* OP_LOADNIL */
	{4,		OP_GETUPVAL,	OpArgU,	OpArgU,	OpArgN,	iABC},		/* OP_GETUPVAL */
	{5,		OP_GETGLOBAL,	OpArgU, OpArgK, OpArgN, iABx},		/* OP_GETGLOBAL */
	{6,		OP_GETTABLE,	OpArgU, OpArgR, OpArgK, iABC},		/* OP_GETTABLE */
	{7,		OP_SETGLOBAL,	OpArgN, OpArgK, OpArgN, iABx},		/* OP_SETGLOBAL */
	{8,		OP_SETUPVAL,	OpArgN, OpArgU, OpArgN, iABC},		/* OP_SETUPVAL */
	{9,		OP_SETTABLE,	OpArgN, OpArgK, OpArgK, iABC},		/* OP_SETTABLE */
	{10,	OP_NEWTABLE,	OpArgU, OpArgU, OpArgU, iABC},		/* OP_NEWTABLE */
	{11,	OP_SELF,		OpArgU, OpArgR, OpArgK, iABC},		/* OP_SELF */
	{12,	OP_ADD,			OpArgU, OpArgK, OpArgK, iABC},		/* OP_ADD */
	{13,	OP_SUB,			OpArgU, OpArgK, OpArgK, iABC},		/* OP_SUB */
	{14,	OP_MUL,			OpArgU, OpArgK, OpArgK, iABC},		/* OP_MUL */
	{15,	OP_DIV,			OpArgU, OpArgK, OpArgK, iABC},		/* OP_DIV */
	{16,	OP_MOD,			OpArgU, OpArgK, OpArgK, iABC},		/* OP_MOD */
	{17,	OP_POW,			OpArgU, OpArgK, OpArgK, iABC},		/* OP_POW */
	{18,	OP_UNM,			OpArgU, OpArgR, OpArgN, iABC},		/* OP_UNM */
	{19,	OP_NOT,			OpArgU, OpArgR, OpArgN, iABC},		/* OP_NOT */
	{20,	OP_LEN,			OpArgU, OpArgR, OpArgN, iABC},		/* OP_LEN */
	{21,	OP_CONCAT,		OpArgU, OpArgR, OpArgR, iABC},		/* OP_CONCAT */
	{22,	OP_JMP,			OpArgN, OpArgR, OpArgN, iAsBx},		/* OP_JMP */
	{23,	OP_EQ,			OpArgN, OpArgK, OpArgK, iABC},		/* OP_EQ */
	{24,	OP_LT,			OpArgN, OpArgK, OpArgK, iABC},		/* OP_LT */
	{25,	OP_LE,			OpArgN, OpArgK, OpArgK, iABC},		/* OP_LE */
	{26,	OP_TEST,		OpArgU, OpArgR, OpArgU, iABC},		/* OP_TEST */
	{27,	OP_TESTSET,		OpArgU, OpArgR, OpArgU, iABC},		/* OP_TESTSET */
	{28,	OP_CALL,		OpArgU, OpArgU, OpArgU, iABC},		/* OP_CALL */
	{29,	OP_TAILCALL,	OpArgU, OpArgU, OpArgU, iABC},		/* OP_TAILCALL */
	{30,	OP_RETURN,		OpArgN, OpArgU, OpArgN, iABC},		/* OP_RETURN */
	{31,	OP_FORLOOP,		OpArgU, OpArgR, OpArgN, iAsBx},		/* OP_FORLOOP */
	{32,	OP_FORPREP,		OpArgU, OpArgR, OpArgN, iAsBx},		/* OP_FORPREP */
	{33,	OP_TFORLOOP,	OpArgN, OpArgN, OpArgU, iABC},		/* OP_TFORLOOP */
	{34,	OP_SETLIST,		OpArgN, OpArgU, OpArgU, iABC},		/* OP_SETLIST */
	{35,	OP_CLOSE,		OpArgN, OpArgN, OpArgN, iABC},		/* OP_CLOSE */
	{36,	OP_CLOSURE,		OpArgU, OpArgU, OpArgN, iABx},		/* OP_CLOSURE */
	{37,	OP_VARARG,		OpArgU, OpArgU, OpArgN, iABC},		/* OP_VARARG */
};

char * AdvanceString(char * str,DWORD len)
{
	char *advstr = new char [len*2];

	memset(advstr,0,len*2*sizeof(char));

	DWORD advindex=0;
	for(DWORD i=0;i<len;i++){
		if(str[i]==0xa)
		{
			advstr[advindex++]='\\';
			advstr[advindex++]='n';
		}else if(str[i]==0x9){
			advstr[advindex++]='\\';
			advstr[advindex++]='t';
		}else if(str[i]=='\\'){
			advstr[advindex++]='\\';
			advstr[advindex++]='\\';
		}else{
			advstr[advindex++]=str[i];
		}
	}

	return advstr;
}

void Note(LuaBinHead* luaHead,OpC* opc,DWORD codeindex,FunctionStruct *fs)
{
#define NEW_NOTE(o,l) \
	o->note=new char[l];\
	memset(o->note,0,l*sizeof(char))

	char * tempstr = NULL;
	char * tempstr2 = NULL;
	//DWORD a,b;

	switch(opc->o)
	{
//MOVE
	case 0:
		break;

//LOADK
	case 1:
		switch(fs->constants.constants_type[opc->Bx])
		{
		case 0:
			NEW_NOTE(opc,4);
			strcpy(opc->note,"nil");
			break;
		case 1:
			NEW_NOTE(opc,7);
			if(((Constants_Type_1*)(fs->constants.constants[opc->Bx]))->is == TRUE)
			{
				strcpy(opc->note,"True");
			}else{
				strcpy(opc->note,"False");
			}
			break;
		case 3:
			NEW_NOTE(opc,30);
			if(luaHead->sizeofnumber==8)
				sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->Bx]))->num));
			else if(luaHead->sizeofnumber==4)
				sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->Bx]))->num));
			break;
		case 4:
			opc->note=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->Bx]))->str,((Constants_Type_4*)(fs->constants.constants[opc->Bx]))->strlength);
			break;
		}
		
		break;

//LOADBOOL
	case 2:
		NEW_NOTE(opc,30);
		if(opc->B==1){
			strcpy(opc->note,"true");	
		}else{
			sprintf(opc->note,"false, to [%d]",codeindex+2);
		}

		break;

//LOADNIL
	case 3:
		break;

//GETUPVAL
	case 4:
		if(opc->B>=fs->upvalues.sizeupvalues)
			break;
		opc->note=AdvanceString((char*)fs->upvalues.upvalues[opc->B].str,fs->upvalues.upvalues[opc->B].strlength);
		break;

//GETGLOBAL
	case 5:
		switch(fs->constants.constants_type[opc->Bx])
		{
		case 0:
			NEW_NOTE(opc,4);
			strcpy(opc->note,"nil");
			break;
		case 1:
			NEW_NOTE(opc,7);
			if(((Constants_Type_1*)(fs->constants.constants[opc->Bx]))->is == TRUE)
			{
				strcpy(opc->note,"True");
			}else{
				strcpy(opc->note,"False");
			}
			break;
		case 3:
			NEW_NOTE(opc,30);
			if(luaHead->sizeofnumber==8)
				sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->Bx]))->num));
			else if(luaHead->sizeofnumber==4)
				sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->Bx]))->num));
			break;
		case 4:
			opc->note=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->Bx]))->str,((Constants_Type_4*)(fs->constants.constants[opc->Bx]))->strlength);
			break;
		}
		break;

//GETTABLE
	case 6:
		if(opc->C<0x100)
			break;

		switch(fs->constants.constants_type[opc->C-0x100])
		{
		case 0:
			NEW_NOTE(opc,4);
			strcpy(opc->note,"nil");
			break;
		case 1:
			NEW_NOTE(opc,7);
			if(((Constants_Type_1*)(fs->constants.constants[opc->C-0x100]))->is == TRUE)
			{
				strcpy(opc->note,"True");
			}else{
				strcpy(opc->note,"False");
			}
			break;
		case 3:
			NEW_NOTE(opc,30);
			if(luaHead->sizeofnumber==8)
				sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
			else if(luaHead->sizeofnumber==4)
				sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
			break;
		case 4:
			tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
			NEW_NOTE(opc,strlen(tempstr)+3);
			sprintf(opc->note,"\"%s\"",tempstr);
			delete tempstr;
			tempstr = NULL;
			break;
		}

		/*tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
		NEW_NOTE(opc,strlen(tempstr)+3);
		sprintf(opc->note,"\"%s\"",tempstr);
		delete tempstr;
		tempstr = NULL;*/
		break;

//SETGLOBAL
	case 7:
		switch(fs->constants.constants_type[opc->Bx])
		{
		case 0:
			NEW_NOTE(opc,4);
			strcpy(opc->note,"nil");
			break;
		case 1:
			NEW_NOTE(opc,7);
			if(((Constants_Type_1*)(fs->constants.constants[opc->Bx]))->is == TRUE)
			{
				strcpy(opc->note,"True");
			}else{
				strcpy(opc->note,"False");
			}
			break;
		case 3:
			NEW_NOTE(opc,30);
			if(luaHead->sizeofnumber==8)
				sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->Bx]))->num));
			else if(luaHead->sizeofnumber==4)
				sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->Bx]))->num));
			break;
		case 4:
			opc->note=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->Bx]))->str,((Constants_Type_4*)(fs->constants.constants[opc->Bx]))->strlength);
			break;
		}
		
		break;

//SETUPVAL
	case 8:
		break;

//SETTABLE
	case 9:
		if(opc->B>=0x100)
		{
			//tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->strlength);

			switch(fs->constants.constants_type[opc->B-0x100])
			{
			case 0:
				tempstr = new char[4];
				strcpy(tempstr,"nil");
				break;
			case 1:
				tempstr = new char[7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->B-0x100]))->is == TRUE)
				{
					strcpy(tempstr,"True");
				}else{
					strcpy(tempstr,"False");
				}
				break;
			case 3:
				tempstr = new char[30];
				if(luaHead->sizeofnumber==8)
					sprintf(tempstr,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(tempstr,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				break;
			case 4:
				tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->strlength);
				break;
			}
		}

		if(opc->C>=0x100)
		{
			switch(fs->constants.constants_type[opc->C-0x100])
			{
			case 0:
				tempstr2 = new char [4];
				strcpy(tempstr2,"nil");
				break;
			case 1:
				tempstr2 = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->C-0x100]))->is == TRUE)
				{
					strcpy(tempstr2,"True");
				}else{
					strcpy(tempstr2,"False");
				}
				break;
			case 3:
				tempstr2 = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(tempstr2,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(tempstr2,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				break;
			case 4:
				tempstr2=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
				break;
			}
		}

		if(tempstr==NULL&&tempstr2!=NULL){
			opc->note = tempstr2;
			tempstr2=NULL;
		}else if(tempstr!=NULL&&tempstr2==NULL){
			NEW_NOTE(opc,strlen(tempstr)+3);
			sprintf(opc->note,"\"%s\"",tempstr);
			delete tempstr;
			tempstr = NULL;
		}else if(tempstr!=NULL&&tempstr2!=NULL){
			NEW_NOTE(opc,strlen(tempstr)+strlen(tempstr2)+6);
			sprintf(opc->note,"%s = ",tempstr);
			if(fs->constants.constants_type[opc->C-0x100]==4)
				sprintf(opc->note,"%s\"%s\"",opc->note,tempstr2);
			else
				sprintf(opc->note,"%s%s",opc->note,tempstr2);
			delete tempstr;
			delete tempstr2;
			tempstr = NULL;
			tempstr2 = NULL;
		}

		break;

//NEWTABLE
	case 10:
		break;

//SELF
	case 11:
		if(opc->C<0x100)
			break;
		tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
		NEW_NOTE(opc,strlen(tempstr)+3);
		sprintf(opc->note,"\"%s\"",tempstr);
		delete tempstr;
		tempstr = NULL;
		break;

//ADD
	case 12:
//SUB
	case 13:
//MUL
	case 14:
//DIV
	case 15:
//MOD
	case 16:
//POW
	case 17:
		if(opc->C<0x100)
			break;
		tempstr = new char [30];
		if(luaHead->sizeofnumber==8)
			sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
		else if(luaHead->sizeofnumber==4)
			sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
		opc->note = tempstr;
		tempstr=NULL;
		break;

//UNM
	case 18:
		break;

//NOT
	case 19:
		break;

//LEN
	case 20:
		break;

//CONCAT
	case 21:
		break;

//JMP
	case 22:
		NEW_NOTE(opc,20);
		sprintf(opc->note,"to [%d]",codeindex+2+opc->Bx);
		break;

//EQ
	case 23:
		if(opc->B>=0x100)
		{
			switch(fs->constants.constants_type[opc->B-0x100])
			{
			case 0:
				tempstr = new char [4];
				strcpy(tempstr,"nil");
				break;
			case 1:
				tempstr = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->B-0x100]))->is == TRUE)
				{
					strcpy(tempstr,"True");
				}else{
					strcpy(tempstr,"False");
				}
				break;
			case 3:
				tempstr = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				break;
			case 4:
				tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->strlength);
				break;
			}
		}

		if(opc->C>=0x100)
		{
			switch(fs->constants.constants_type[opc->C-0x100])
			{
			case 0:
				tempstr2 = new char [4];
				strcpy(tempstr2,"nil");
				break;
			case 1:
				tempstr2 = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->C-0x100]))->is == TRUE)
				{
					strcpy(tempstr2,"True");
				}else{
					strcpy(tempstr2,"False");
				}
				break;
			case 3:
				tempstr2 = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				break;
			case 4:
				tempstr2=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
				break;
			}
		}

		if(tempstr!=NULL&&tempstr2==NULL)
		{
			NEW_NOTE(opc,strlen(tempstr)+40);
			if(fs->constants.constants_type[opc->B-0x100]==4)
				sprintf(opc->note,"\"%s\"== to [%d] if true",tempstr,codeindex+2+1);
			else
				sprintf(opc->note,"%s== to [%d] if true",tempstr,codeindex+2+1);

			delete tempstr;
			tempstr = NULL;
		}
		else if(tempstr==NULL&&tempstr2!=NULL)
		{
			NEW_NOTE(opc,strlen(tempstr2)+40);
			if(fs->constants.constants_type[opc->C-0x100]==4)
				sprintf(opc->note,"==\"%s\" to [%d] if true",tempstr2,codeindex+2+1);
			else
				sprintf(opc->note,"==%s to [%d] if true",tempstr2,codeindex+2+1);

			delete tempstr2;
			tempstr2 = NULL;
		}
		else if(tempstr!=NULL&&tempstr2!=NULL)
		{
			NEW_NOTE(opc,strlen(tempstr)+strlen(tempstr2)+40);
			if(fs->constants.constants_type[opc->B-0x100]==4&&fs->constants.constants_type[opc->C-0x100]!=4)
			{
				sprintf(opc->note,"\"%s\"==%s to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]!=4&&fs->constants.constants_type[opc->C-0x100]==4)
			{
				sprintf(opc->note,"%s==\"%s\" to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]==4&&fs->constants.constants_type[opc->C-0x100]==4)
			{
				sprintf(opc->note,"\"%s\"==\"%s\" to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]!=4&&fs->constants.constants_type[opc->C-0x100]!=4)
			{
				sprintf(opc->note,"%s==%s to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}

			delete tempstr;
			tempstr = NULL;
			delete tempstr2;
			tempstr2 = NULL;
		}
		else
		{
			NEW_NOTE(opc,40);
			sprintf(opc->note,"to [%d] if true",codeindex+2+1);
		}
		
		break;

//LT
	case 24:
		if(opc->B>=0x100)
		{
			switch(fs->constants.constants_type[opc->B-0x100])
			{
			case 0:
				tempstr = new char [4];
				strcpy(tempstr,"nil");
				break;
			case 1:
				tempstr = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->B-0x100]))->is == TRUE)
				{
					strcpy(tempstr,"True");
				}else{
					strcpy(tempstr,"False");
				}
				break;
			case 3:
				tempstr = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				break;
			case 4:
				tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->strlength);
				break;
			}
		}

		if(opc->C>=0x100)
		{
			switch(fs->constants.constants_type[opc->C-0x100])
			{
			case 0:
				tempstr2 = new char [4];
				strcpy(tempstr2,"nil");
				break;
			case 1:
				tempstr2 = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->C-0x100]))->is == TRUE)
				{
					strcpy(tempstr2,"True");
				}else{
					strcpy(tempstr2,"False");
				}
				break;
			case 3:
				tempstr2 = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				break;
			case 4:
				tempstr2=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
				break;
			}
		}

		if(tempstr!=NULL&&tempstr2==NULL)
		{
			NEW_NOTE(opc,strlen(tempstr)+40);
			if(fs->constants.constants_type[opc->B-0x100]==4)
				sprintf(opc->note,"\"%s\"< to [%d] if true",tempstr,codeindex+2+1);
			else
				sprintf(opc->note,"%s< to [%d] if true",tempstr,codeindex+2+1);

			delete tempstr;
			tempstr = NULL;
		}
		else if(tempstr==NULL&&tempstr2!=NULL)
		{
			NEW_NOTE(opc,strlen(tempstr2)+40);
			if(fs->constants.constants_type[opc->C-0x100]==4)
				sprintf(opc->note,"<\"%s\" to [%d] if true",tempstr2,codeindex+2+1);
			else
				sprintf(opc->note,"<%s to [%d] if true",tempstr2,codeindex+2+1);

			delete tempstr2;
			tempstr2 = NULL;
		}
		else if(tempstr!=NULL&&tempstr2!=NULL)
		{
			NEW_NOTE(opc,strlen(tempstr)+strlen(tempstr2)+40);
			if(fs->constants.constants_type[opc->B-0x100]==4&&fs->constants.constants_type[opc->C-0x100]!=4)
			{
				sprintf(opc->note,"\"%s\"<%s to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]!=4&&fs->constants.constants_type[opc->C-0x100]==4)
			{
				sprintf(opc->note,"%s<\"%s\" to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]==4&&fs->constants.constants_type[opc->C-0x100]==4)
			{
				sprintf(opc->note,"\"%s\"<\"%s\" to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]!=4&&fs->constants.constants_type[opc->C-0x100]!=4)
			{
				sprintf(opc->note,"%s<%s to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}

			delete tempstr;
			tempstr = NULL;
			delete tempstr2;
			tempstr2 = NULL;
		}
		else
		{
			NEW_NOTE(opc,40);
			sprintf(opc->note,"to [%d] if true",codeindex+2+1);
		}
		
		break;
//LE
	case 25:
		if(opc->B>=0x100)
		{
			switch(fs->constants.constants_type[opc->B-0x100])
			{
			case 0:
				tempstr = new char [4];
				strcpy(tempstr,"nil");
				break;
			case 1:
				tempstr = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->B-0x100]))->is == TRUE)
				{
					strcpy(tempstr,"True");
				}else{
					strcpy(tempstr,"False");
				}
				break;
			case 3:
				tempstr = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->B-0x100]))->num));
				break;
			case 4:
				tempstr=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->B-0x100]))->strlength);
				break;
			}
		}

		if(opc->C>=0x100)
		{
			switch(fs->constants.constants_type[opc->C-0x100])
			{
			case 0:
				tempstr2 = new char [4];
				strcpy(tempstr2,"nil");
				break;
			case 1:
				tempstr2 = new char [7];
				if(((Constants_Type_1*)(fs->constants.constants[opc->C-0x100]))->is == TRUE)
				{
					strcpy(tempstr2,"True");
				}else{
					strcpy(tempstr2,"False");
				}
				break;
			case 3:
				tempstr2 = new char [30];
				if(luaHead->sizeofnumber==8)
					sprintf(opc->note,"%g",*((double*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				else if(luaHead->sizeofnumber==4)
					sprintf(opc->note,"%g",*((float*)((Constants_Type_3*)(fs->constants.constants[opc->C-0x100]))->num));
				break;
			case 4:
				tempstr2=AdvanceString((char*)((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->str,((Constants_Type_4*)(fs->constants.constants[opc->C-0x100]))->strlength);
				break;
			}
		}

		if(tempstr!=NULL&&tempstr2==NULL)
		{
			NEW_NOTE(opc,strlen(tempstr)+40);
			if(fs->constants.constants_type[opc->B-0x100]==4)
				sprintf(opc->note,"\"%s\"> to [%d] if true",tempstr,codeindex+2+1);
			else
				sprintf(opc->note,"%s> to [%d] if true",tempstr,codeindex+2+1);

			delete tempstr;
			tempstr = NULL;
		}
		else if(tempstr==NULL&&tempstr2!=NULL)
		{
			NEW_NOTE(opc,strlen(tempstr2)+40);
			if(fs->constants.constants_type[opc->C-0x100]==4)
				sprintf(opc->note,">\"%s\" to [%d] if true",tempstr2,codeindex+2+1);
			else
				sprintf(opc->note,">%s to [%d] if true",tempstr2,codeindex+2+1);

			delete tempstr2;
			tempstr2 = NULL;
		}
		else if(tempstr!=NULL&&tempstr2!=NULL)
		{
			NEW_NOTE(opc,strlen(tempstr)+strlen(tempstr2)+40);
			if(fs->constants.constants_type[opc->B-0x100]==4&&fs->constants.constants_type[opc->C-0x100]!=4)
			{
				sprintf(opc->note,"\"%s\">%s to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]!=4&&fs->constants.constants_type[opc->C-0x100]==4)
			{
				sprintf(opc->note,"%s>\"%s\" to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]==4&&fs->constants.constants_type[opc->C-0x100]==4)
			{
				sprintf(opc->note,"\"%s\">\"%s\" to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}
			else if(fs->constants.constants_type[opc->B-0x100]!=4&&fs->constants.constants_type[opc->C-0x100]!=4)
			{
				sprintf(opc->note,"%s>%s to [%d] if true",tempstr,tempstr2,codeindex+2+1);
			}

			delete tempstr;
			tempstr = NULL;
			delete tempstr2;
			tempstr2 = NULL;
		}
		else
		{
			NEW_NOTE(opc,40);
			sprintf(opc->note,"to [%d] if true",codeindex+2+1);
		}
		
		break;

//TEST
	case 26:
		break;
//TESTSET
	case 27:
		break;
//CALL
	case 28:
		break;

//TAILCALL
	case 29:
		break;

//RETURN
	case 30:
		break;

//FORLOOP
	case 31:
		NEW_NOTE(opc,80);
		sprintf(opc->note,"to [%d] if loop",codeindex+2+opc->Bx);
		break;

//FORPREP
	case 32:
		NEW_NOTE(opc,20);
		sprintf(opc->note,"to [%d]",codeindex+2+opc->Bx);
		break;

//TFORREP
	case 33:
		break;

//SETLIST
	case 34:
		NEW_NOTE(opc,20);
		if(opc->B!=0)
			sprintf(opc->note,"index %d to %d",opc->C,opc->B);
		else
			sprintf(opc->note,"index %d to Top",opc->C);
		break;

//CLOSE
	case 35:
		break;

//CLOSURE
	case 36:
		break;

//VARAGE
	case 37:
		break;
	default:
		break;
	}
}

OpC * AnalysisCode(LuaBinHead* luaHead,FunctionStruct *fs)
{
	FunctionCode *fc = &(fs->code);

	OpC * opc = NULL;
	
	//check
	int temp;

	if(fc->sizecode==0)
		return NULL;

	opc = new OpC[fc->sizecode];

	for(DWORD i=0;i<fc->sizecode;i++)
	{
		opc[i].code = fc->code[i];
		opc[i].o = GetOpcode(fc->code[i]);
		opc[i].A = GetA(fc->code[i]);
		
		if(OpCodes_Define[opc[i].o].mode==iABC)
		{
			opc[i].B = GetB(fc->code[i]);
			opc[i].C = GetC(fc->code[i]);
		}else{
			temp = GetBx(fc->code[i]);

			if(temp<0x10000)
				opc[i].Bx = temp;
			else
				//opc[i].Bx = -(0xFFFF-(temp&0xFFFF));
				opc[i].Bx = (short)temp+1;
		}

		opc[i].note = NULL;
		Note(luaHead,&(opc[i]),i,fs);
	}

	return opc;
}
